package com.woniuxy.consumer;

import com.rabbitmq.client.Channel;
import com.woniuxy.mapper.OrdersMapper;
import com.woniuxy.util.RabbitMQConst;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
@Transactional
@Component
public class DeleteUserOrderConsumer {
    @Resource
    private OrdersMapper ordersMapper;
    @RabbitListener(queues = RabbitMQConst.DELETE_USER_ORDER_QUEUE)
  public  void receive(String msg, Channel channel, Message message){
        try {
            //根据jwt获得用户id
            //Integer userId = UserContext.getUserId();
           String orderId=msg.substring(0,msg.length()-2);
           Integer  userId=Integer.parseInt(msg.substring(msg.length()-1));

            System.out.println(orderId);
            System.out.println(userId);
            ordersMapper.deleteUserOrderItemByOrderId(orderId);
            ordersMapper.deleteUserOrderByOrderId(orderId,userId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);//确认消息
        } catch (Exception e) {
            e.printStackTrace();
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);//拒绝消息
            } catch (IOException ex) {
                e.printStackTrace();
            }
        }

    }
}
